1.16 Type Casting in C Programming

Module 1.16 • Automatic Promotions, Cast Operators, Precision Losses & Pointer Conversions

1.16.1 Introduction

In C programming, different variables can store different types of data such as integers, floating-point numbers, characters, and double-precision values.

Sometimes, it becomes necessary to convert one data type into another. This conversion process is known as Type Casting.

Type casting helps programmers:

1.16.2 What is Type Casting?

Type casting is the process of converting a value from one data type to another.

For example:

int num = 25;
float value;
 
value = num;

Here, the integer value 25 is converted into the floating-point value 25.0.

1.16.3 Why Type Casting is Required

Consider the following situation:

int totalMarks = 450;
int subjects = 6;
 
float average;
 
average = totalMarks / subjects;

Output:

75.000000

Although the result appears correct, the division is performed as integer division.

Now consider:

int totalMarks = 455;
int subjects = 6;
 
float average;
 
average = totalMarks / subjects;

Output:

75.000000

Expected:

75.833333

To obtain the correct answer, type casting is required.

average = (float)totalMarks / subjects;

Output:

75.833333

1.16.4 Types of Type Casting

Type casting can be classified into two categories:

  1. Implicit Type Casting
  2. Explicit Type Casting

1.16.5 Implicit Type Casting

Implicit type casting is automatically performed by the compiler. It usually occurs when a smaller data type is assigned to a larger data type.

Example:

#include<stdio.h>
 
int main()
{
    int age = 28;
    float value;
 
    value = age;
 
    printf("%.2f", value);
 
    return 0;
}

Output:

28.00

The compiler automatically converts the integer value into a floating-point value.

1.16.6 Example of Automatic Conversion

#include<stdio.h>
 
int main()
{
    char grade = 'B';
    int asciiValue;
 
    asciiValue = grade;
 
    printf("%d", asciiValue);
 
    return 0;
}

Output:

66

The character is automatically converted into its ASCII value.

1.16.7 Data Type Promotion

When different data types participate in an expression, C automatically promotes smaller types to larger types.

Example:

#include<stdio.h>
 
int main()
{
    int num = 12;
    double rate = 4.75;
 
    double result;
 
    result = num + rate;
 
    printf("%.2lf", result);
 
    return 0;
}

Output:

16.75

The integer value is automatically promoted to double before addition.

1.16.8 Explicit Type Casting

Explicit type casting is performed manually by the programmer.

Syntax:

(type) expression

General Form:

variable = (datatype) value;

1.16.9 Float to Integer Conversion

#include<stdio.h>
 
int main()
{
    float price = 89.95;
 
    int amount;
 
    amount = (int)price;
 
    printf("%d", amount);
 
    return 0;
}

Output:

89

The decimal portion is removed.

1.16.10 Double to Integer Conversion

#include<stdio.h>
 
int main()
{
    double number = 456.789;
 
    int result;
 
    result = (int)number;
 
    printf("%d", result);
 
    return 0;
}

Output:

456

Only the integer part remains.

1.16.11 Integer to Character Conversion

#include<stdio.h>
 
int main()
{
    int value = 67;
 
    char ch;
 
    ch = (char)value;
 
    printf("%c", ch);
 
    return 0;
}

Output:

C

ASCII value 67 corresponds to character C.

1.16.12 Character to Integer Conversion

#include<stdio.h>
 
int main()
{
    char ch = 'M';
 
    int code;
 
    code = (int)ch;
 
    printf("%d", code);
 
    return 0;
}

Output:

77

1.16.13 Type Casting in Arithmetic Operations

Without Casting:

#include<stdio.h>
 
int main()
{
    int a = 17;
    int b = 4;
 
    float result;
 
    result = a / b;
 
    printf("%.2f", result);
 
    return 0;
}

Output:

4.00

With Casting:

#include<stdio.h>
 
int main()
{
    int a = 17;
    int b = 4;
 
    float result;
 
    result = (float)a / b;
 
    printf("%.2f", result);
 
    return 0;
}

Output:

4.25

1.16.14 Calculating Percentage Using Type Casting

#include<stdio.h>
 
int main()
{
    int obtained = 523;
    int maximum = 600;
 
    float percentage;
 
    percentage = ((float)obtained / maximum) * 100;
 
    printf("%.2f%%", percentage);
 
    return 0;
}

Output:

87.17%

1.16.15 Type Casting and Memory Representation

Consider:

float value = 12.75;

Memory stores:

12.75

After casting:

(int)value

Result:

12

The fractional portion is discarded.

1.16.16 Precision Loss

One important issue during type conversion is precision loss.

Example:

#include<stdio.h>
 
int main()
{
    double amount = 987.654321;
 
    float value;
 
    value = (float)amount;
 
    printf("%.6f", value);
 
    return 0;
}

Output may be:

987.654297

Some precision is lost because float stores fewer digits than double.

1.16.17 Overflow During Casting

Improper casting may produce incorrect results.

Example:

#include<stdio.h>
 
int main()
{
    int num = 300;
 
    char ch;
 
    ch = (char)num;
 
    printf("%d", ch);
 
    return 0;
}

Output depends on the compiler and system because the value exceeds the storage capacity of a char.

1.16.18 Type Casting with Pointers

Pointers often require explicit type conversion.

Example:

#include<stdio.h>
 
int main()
{
    int num = 150;
 
    void *ptr;
 
    ptr = &num;
 
    printf("%d", *(int*)ptr);
 
    return 0;
}

Output:

150

The void pointer is converted back into an integer pointer before dereferencing.

1.16.19 What is a Void Pointer?

A void pointer can store the address of any data type.

Example:

void *ptr;

Possible assignments:

int x = 25;
float y = 6.5;
 
ptr = &x;
ptr = &y;

However, type casting is required before accessing the value.

1.16.20 Implicit vs Explicit Type Casting

Feature Implicit Casting Explicit Casting
Performed ByCompilerProgrammer
Syntax RequiredNoYes
Data Loss RiskLowPossible
ControlAutomaticManual
UsageCompatible TypesDifferent Types

1.16.21 Common Data Conversions

Source Type Target Type Casting Required
intfloatOptional
intdoubleOptional
floatintRequired
doubleintRequired
charintOptional
intcharRecommended
void*Any PointerRequired

1.16.22 Real-World Applications

Type casting is commonly used in:

1.16.23 Best Practices

Use Explicit Casting When Needed

(float)total makes the intention clear.

Avoid Unnecessary Casts

Bad: int a = (int)10; — The cast is unnecessary.

Watch for Precision Loss

double -> float, float -> int, long -> short may lose data.

Validate Range Before Casting

Ensure the value fits inside the destination type.

Example:

int num = 120;
 
char ch = (char)num;

Safe because 120 fits within the char range on most systems.

1.16.24 Advantages of Type Casting

1.16.25 Disadvantages of Improper Casting

Summary

Verify Comprehension: Technical Knowledge Assessment

Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.